package org.hilo.boot.app.config;

import org.hilo.boot.core.PageHelperProperties;
import org.hilo.boot.core.PerformanceInterceptor;
import org.hilo.boot.core.value.AutoValueInit;
import org.hilo.boot.core.value.FastObjectInitValue;
import org.hilo.boot.core.value.FieldInitValueChain;
import org.hilo.boot.core.value.ObjectInitValue;
import org.hilo.boot.core.value.ObjectInitValueChain;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.github.pagehelper.PageInterceptor;

/**
 * Mybatis增强配置
 * 
 * @author zollty
 * @since 2017-4-25
 */
@Configuration
@EnableConfigurationProperties(PageHelperProperties.class)
//@tk.mybatis.spring.annotation.MapperScan({"com.xxx.**.dao"})
public class MybatisConfig {
    
    /**
     * 分页插件
     */
    @Bean
    PageInterceptor pageInterceptor(PageHelperProperties properties) {
        PageInterceptor page = new PageInterceptor();
        page.setProperties(properties.getProperties());
        return page;
    }
    
    /**
     * <p>性能监控插件</p>
     * <p>注意：这个要放在 其他插件之后，才行。</p>
     */
    @Bean
    PerformanceInterceptor performanceInterceptor() {
        return new PerformanceInterceptor();
    }
    
//  @Autowired
//  private List<SqlSessionFactory> sqlSessionFactoryList;
//
//  @PostConstruct
//  public void addPageInterceptor() {
//      Interceptor interceptor = new PerformanceInterceptor();
//      new PerformanceInterceptor();
//      for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
//          sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
//      }
//  }
    
    /**
     * 默认的AutoValueInit
     */
    @Bean
    AutoValueInit autoValueInit() {
        return new AutoValueInit();
    }
    
    /**
     * Entity Field值初始化
     */
    @Bean
    FieldInitValueChain fieldInitValueChain() {
        FieldInitValueChain chain = new FieldInitValueChain();
        chain.addLast(autoValueInit());
        return chain;
    }
    
    /**
     * Entity对象值初始化
     */
    @Bean
    ObjectInitValue objInit() {
        FastObjectInitValue biv = new FastObjectInitValue(fieldInitValueChain());
        ObjectInitValueChain ret = new ObjectInitValueChain();
        ret.addLast(biv);
        return ret;
    }
    
}
